package main

import (
	"bytes"
	_ "embed"
	"fmt"
	"github.com/faiface/beep"
	"github.com/faiface/beep/mp3"
	"github.com/faiface/beep/speaker"
	"log"
	"os"
	"time"
)

//go:embed `Venom`
var fs []byte

type MyFile struct {
	*bytes.Reader
	mif myFileInfo
}

type myFileInfo struct {
	name string
	data []byte
}

func (mif myFileInfo) Name() string       { return mif.name }
func (mif myFileInfo) Size() int64        { return int64(len(mif.data)) }
func (mif myFileInfo) Mode() os.FileMode  { return 0444 }        // Read for all
func (mif myFileInfo) ModTime() time.Time { return time.Time{} } // Return anything
func (mif myFileInfo) IsDir() bool        { return false }
func (mif myFileInfo) Sys() interface{}   { return nil }

func (mf *MyFile) Close() error { return nil } // Noop, nothing to do

func (mf *MyFile) Readdir(count int) ([]os.FileInfo, error) {
	return nil, nil // We are not a directory but a single file
}

func (mf *MyFile) Stat() (os.FileInfo, error) {
	return mf.mif, nil
}

func main() {

	var total time.Duration = 10
	go func() {
		i := 1
		for {
			select {
			case <-time.After(time.Second):
				i += 1
				if 600-i < 0 {
					fmt.Println("牛棚大作战开始了~~")
					fmt.Println("牛棚大作战开始了~~")
					fmt.Println("牛棚大作战开始了~~")
					return
				}
				n := 600 - i
				m := n / 60
				s := n % 60
				if m != 0 {
					fmt.Printf("剩余时间 %d 分 %d 秒 \n", m, s)
				} else {
					fmt.Printf("剩余时间 %d 秒 \n", s)
				}
			}
		}
	}()
	for {
		select {
		case <-time.Tick(time.Minute * total):
			play()
		}
	}

}
func play() {
	mf := &MyFile{
		Reader: bytes.NewReader(fs),
		mif: myFileInfo{
			name: "Venom",
			data: fs,
		},
	}
	audioStreamer, format, err := mp3.Decode(mf)
	if err != nil {
		log.Fatal(err)
	}
	defer audioStreamer.Close()
	_ = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
	done := make(chan bool)
	speaker.Play(beep.Seq(audioStreamer, beep.Callback(func() {
		done <- true
	})))
	<-done
}
